// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package version

import (
	"cmp"
	"fmt"
	"sync"
)

var _ fmt.Stringer = (*Application)(nil)

type Application struct {
	Name  string `json:"name"  yaml:"name"`
	Major int    `json:"major" yaml:"major"`
	Minor int    `json:"minor" yaml:"minor"`
	Patch int    `json:"patch" yaml:"patch"`

	makeStrOnce sync.Once
	str         string
}

func (a *Application) String() string {
	a.makeStrOnce.Do(a.initString)
	return a.str
}

func (a *Application) initString() {
	a.str = fmt.Sprintf(
		"%s/%d.%d.%d",
		a.Name,
		a.Major,
		a.Minor,
		a.Patch,
	)
}

// Compare returns
//
//	-1 if a is less than o,
//	 0 if a equals o,
//	+1 if a is greater than o.
func (a *Application) Compare(o *Application) int {
	if c := cmp.Compare(a.Major, o.Major); c != 0 {
		return c
	}
	if c := cmp.Compare(a.Minor, o.Minor); c != 0 {
		return c
	}
	return cmp.Compare(a.Patch, o.Patch)
}
